import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useCallback, useMemo } from 'react';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang';

/**
 * 获取全生命周期
 * @param callback 接口回调
 * @returns
 */
const getLifeCycleService = (callback: (res: any, clickType: boolean) => void) => {
  const getLifeCycle = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/assetInfo/getAssetCycle`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    getLifeCyclelRun: getLifeCycle?.run,
    getLifeCyclelLoading: getLifeCycle?.loading,
  };
};

/** 获取修改记录 */
const getChangeLogService = (callback: (res: any, clickType: boolean) => void) => {
  /** 获取列表数据 */
  const { tableProps, data, error, loading, run, params } = useRequest(
    (opt: any) => {
      let personUrl = `${baseUrl}/assetInfoLog/list`;
      let personBody: any = {
        order: 'desc',
        page: 1,
        rows: 10,
        params: 'assetInfoId',
        sort: 'createTime',
        ...opt,
      };
      return requestFunComponFiex('POST', personUrl, personBody);
    },
    {
      manual: true, //页面初始请求为true
      paginated: true,
      defaultPageSize: 10,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );

  /* 获取页面数据 */
  const getChangeLogListTableData = useCallback((otherRequest: any) => {
    const request: any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  }, []);

  /* 页面列表 */
  let tableValues = useMemo(() => {
    return {
      changeLogData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getChangeLogListTableData, //其他请求操作方法例如刷新等等
    otherParams: params?.[0],
    ...tableValues, //页面列表值
  };
};

export { getLifeCycleService, getChangeLogService };
